<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */

class upload extends entity {

	var $id;
	var $userid;
	var $udate;
	var $fsize;
	var $fname;
	var $teamid;
	var $projectid;
	var $filesid = array();
	var $status;
	var $filelist;
	var $takeuser;
	var $takedate;
		
    public function __construct()
    {
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		return new upload;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".UPLOAD." WHERE hash = ".util::str($this->id);
		return $sql;
	}

	public function listAll() {
		$sql = "SELECT * FROM ".UPLOAD." ORDER BY date DESC";
		return $sql;
	}

	public function listByTeam($teamid) {
		$sql = "SELECT * FROM ".UPLOAD." WHERE status = '1' and teamid=".$teamid." ORDER BY date DESC";
		return $sql;
	}

	public function listOrphans() {
		$expire = util::formatDate(time()-(24*60*60));
		$sql = "SELECT * FROM ".UPLOAD." WHERE status = '0' and date<=".$expire;
		return $sql;
	}

	public function listExpired() {
		$expire = util::formatDate(time()-(UPLOAD_EXPIRE*24*60*60));
		$sql = "SELECT * FROM ".UPLOAD." WHERE date<=".$expire;
		return $sql;
	}
	
	public function insert() {
		$files = util::arrayToString($this->filesid);
		$sql = "INSERT INTO ".UPLOAD." (hash, userid, date, filesize, status, filename, teamid, projectid, files) ";
		$sql .= "VALUES (";
		$sql .=	util::str($this->id);
		$sql .=	",".util::str($this->userid);
		$sql .=	",".util::str(util::now());
		$sql .=	",".util::str($this->fsize);
		$sql .=	",".util::str(UPLOAD_NEW);
		$sql .=	",".util::str($this->fname);
		$sql .=	",".util::str($this->teamid);
		$sql .=	",".util::str($this->projectid);
		$sql .=	",".util::str($files);
		$sql .= ")";
		return $sql;
	}

	public function update() {
		$sql = "UPDATE ".UPLOAD. " set status=".util::str(UPLOAD_OK)." WHERE hash=".util::str($this->id);
		return $sql;
	}

	public function updateFlag() {
		$takedate = $this->takedate == null ? "NULL" : util::str($this->takedate);
		$takeuser = $this->takeuser == null ? "NULL" : util::str($this->takeuser);
		$sql = "UPDATE ".UPLOAD." set takeuser=".$takeuser.", takedate=".$takedate." WHERE hash=".util::str($this->id);
		return $sql;
	}
	
	public function delete() {
		$sql = "DELETE FROM ".UPLOAD. " WHERE hash=".util::str($this->id);
		return $sql;
	}
	
	public function addFile($f) {
		array_push($this->filesid,$f);
	}

	public function removeFile($f) {
		$n = array();
		for($i=0;$i<count($this->filesid);$i++) {
			if ($this->filesid[$i]!=$f) {
				array_push($n,$this->filesid[$i]);
			}
		}
		$this->filesid=$n;
	}		
	
	public function fill($array) {
		$this->id=$array['hash'];
		$this->userid=$array['userid'];
		$this->udate=$array['date'];
		$this->fsize=$array['filesize'];
		$this->fname=$array['filename'];
		$this->teamid=$array['teamid'];
		$this->projectid=$array['projectid'];
		$this->filesid=util::stringToArray($array['files']);
		$this->status=$array['status'];
		$this->takeuser=$array['takeuser'];
		$this->takedate=$array['takedate'];
		
		if ($this->takedate!=null and util::isExpire($this->takedate,TAKE_TIMEOUT)) {
			$this->takeuser=null;
			$this->takedate=null;
		}
		
	}
	
}

?>
